package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.TaskLanguage;
import com.novax.ex.activity.infrastructure.entity.TaskTemplate;
import com.novax.ex.activity.open.api.inner.TaskTemplateApi;
import com.novax.ex.activity.open.model.request.task.TaskTemplateLanguageRequest;
import com.novax.ex.activity.open.model.request.task.TaskTemplateRequest;
import com.novax.ex.activity.open.model.response.task.TaskTemplateLanguageResponse;
import com.novax.ex.activity.open.model.response.task.TaskTemplateResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.TaskLanguageService;
import com.novax.ex.activity.provider.service.TaskTemplateService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.enums.TaskLanguageType;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 后台-任务名称配置
 * @Author ChenXi
 * @Date 2023/06/21/10:34
 */

@RestController
public class TaskTemplateController extends BaseActivityController implements TaskTemplateApi {

    @Resource
    private TaskTemplateService taskTemplateService;

    @Resource
    private TaskLanguageService taskLanguageService;

    @Override
    public ReturnResult<List<TaskTemplateResponse>> queryAll() {
        return ReturnResult.success(CopyUtils.copyList(taskTemplateService.queryAll(), TaskTemplateResponse.class));
    }

    @Override
    public ReturnResult<?> modify(String language, Long id, TaskTemplateRequest taskTemplateRequest) {
        // 只有任务1 关注Twitter和任务2 加入telegram两个任务可以修改h5链接
        if(id != 1 && id != 2){
            return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
        }

        String h5Url = taskTemplateRequest.getH5Url();
        TaskTemplate taskTemplate = new TaskTemplate();
        taskTemplate.setId(id);
        taskTemplate.setH5Url(h5Url);
        boolean flag = taskTemplateService.updateH5Url(taskTemplate);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<List<TaskTemplateLanguageResponse>> queryAllByTitle(Long id) {
        return ReturnResult.success(CopyUtils.copyList(taskLanguageService.findByParentId(id, TaskLanguageType.task_template.getType()), TaskTemplateLanguageResponse.class));
    }

    @Override
    public ReturnResult<?> modify(String language, Long id, TaskTemplateLanguageRequest taskTemplateLanguageRequest) {
        // 语言不可更改
        taskTemplateLanguageRequest.setLanguage(null);
        TaskLanguage taskLanguage = CopyUtils.copyObject(taskTemplateLanguageRequest, TaskLanguage.class);
        taskLanguage.setId(id);
        boolean flag = taskLanguageService.modifySelective(taskLanguage);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> add(String language, Long id, TaskTemplateLanguageRequest taskTemplateLanguageRequest,
                               BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }

        Map<String, Object> query = new HashMap<>(2);
        query.put("language", taskTemplateLanguageRequest.getLanguage());
        query.put("parent_id", id);
        query.put("type", TaskLanguageType.task_template.getType());
        List<TaskLanguage> list = taskLanguageService.findList(query);

        // 新增时判断语言是否已配置
        if(!list.isEmpty()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LANGUAGE_EXIST));
        }

        TaskLanguage taskLanguage = CopyUtils.copyObject(taskTemplateLanguageRequest, TaskLanguage.class);
        taskLanguage.setType(TaskLanguageType.task_template.getType());
        taskLanguage.setParentId(id);
        boolean flag = taskLanguageService.addSelective(taskLanguage);;
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

}
